# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import platform

import pymongo

from girl.items.city import *
from girl.items.city import girlItem as girl
from girl.items.dmm import *
from girl.items.hotel import *
from girl.items import poke
from girl.items import night
from girl.items import host2  # from scrapy.pipelines.images import ImagesPipeline


class GirlPipeline(object):
    def process_item(self, item, spider):
        return item

        # class MyImagesPipeline(ImagesPipeline):
        #     def redirect(self,file_url):
        #         url = "https://www.cityheaven.net/miyagi/A0401/A040101/rent/"
        #         response = req.head(file_url,headers={"Referer":url})
        #         if response.status_code in (302,301):
        #             file_url = response.headers["Location"]
        #         return file_url
        #     def get_media_requests(self, item, info):
        #         url = "https://www.cityheaven.net/miyagi/A0401/A040101/rent/"
        #         try:
        #             if item.get(self.images_urls_field):
        #                 urls = map(self.redirect,item.get(self.images_urls_field, []))
        #                 return [Request(x,headers={"Referer":url}) for x in urls]
        #         except Exception, e:
        #             # import ipdb;ipdb.set_trace()
        #             pass
        #         return

        # def media_downloaded(self, response, request, info):
        #     if response.status in (301,302):
        #         tmp = req.get(response.headers["Location"])
        #         # import ipdb;ipdb.set_trace()
        #         # response = response.replace(url=tmp.url,body=tmp.content,status=tmp.status_code)
        #         yield scrapy.Request(tmp.url)
        #         super(MyImagesPipeline,self).media_downloaded(response, request, info)
        # def item_completed(self, results, item, info):
        #     # import ipdb;ipdb.set_trace()
        #     try:
        #     if isinstance(item, dict) or self.images_result_field in item.fields:
        #         item[self.images_result_field] = []
        #         if item[self.images_urls_field]:
        #             for x in item[self.images_urls_field]:
        #                 item[self.images_result_field].append(hashlib.sha1(to_bytes(x)).hexdigest())
        #         # item[self.images_result_field] = [x for ok, x in results if ok]
        #     return item


class MongoPipeline(object):
    def __init__(self):
        if platform.system() != 'Darwin':
            mongoIp = '127.0.0.1'
        else:
            mongoIp = '47.75.39.50'
        self.client = pymongo.MongoClient(mongoIp, socketKeepAlive=True, maxPoolSize=200)
        self.client.admin.authenticate("admin321", "dsf::6666,,<<", mechanism='SCRAM-SHA-1')
        self.db = self.client.city4
        # pool = redis.ConnectionPool(host='localhost', port=6379, db=1)
        # conn = redis.Redis(connection_pool=pool)

    def process_item(self, item, spider):
        if spider.name == "host2":
            return self.host2_item(item, spider)
        elif spider.name.startswith("dmm"):
            return self.dmm_item(item, spider)
        elif spider.name.startswith("hotel"):
            return self.hotel_item(item, spider)
        elif spider.name.startswith("poke"):
            return self.poke_item(item, spider)
        elif spider.name.startswith("night"):
            return self.night_item(item, spider)
        if isinstance(item, storesItem):
            self.db.stores.insert(dict(item))
        elif isinstance(item, diaryItem):
            self.db.diary.insert(dict(item))
        elif isinstance(item, girl):
            self.db.girl.insert(dict(item))
        elif isinstance(item, movieItem):
            self.db.movie.insert(dict(item))
        elif isinstance(item, emailItem):
            self.db.email.insert(dict(item))
        return item

    def dmm_item(self, item, spider):
        db = self.client.dmm2
        if isinstance(item, dmmimgItem):
            db.img.insert(dict(item))
        elif isinstance(item, avgirlItem):
            db.avgirl.insert(dict(item))
        else:
            db = self.client.dmm
            db.girl.insert(dict(item))
        return item

    def poke_item(self, item, spider):
        db = self.client.poke
        if isinstance(item, poke.girlItem):
            db.girl.insert(dict(item))
        elif isinstance(item, poke.clubItem):
            db.club.insert(dict(item))
        elif isinstance(item, poke.blogItem):
            db.blog.insert(dict(item))
        elif isinstance(item, poke.workItem):
            db.work.insert(dict(item))
        return item

    def night_item(self, item, spider):
        db = self.client.night
        if isinstance(item, night.girlItem):
            db.girl.insert(dict(item))
        elif isinstance(item, night.storeItem):
            db.store.insert(dict(item))
        elif isinstance(item, night.blogItem):
            db.blog.insert(dict(item))
        elif isinstance(item, night.workItem):
            db.work.insert(dict(item))
        elif isinstance(item, night.movieItem):
            db.movie.insert(dict(item))
        return item

    def hotel_item(self, item, spider):
        db = self.client.hotel
        if isinstance(item, hotelItem):
            db.hotel.insert(dict(item))
        elif isinstance(item, roomItem):
            db.room.insert(dict(item))
        return item

    def host2_item(self, item, spider):
        db = self.client.host2
        if isinstance(item, host2.storesItem):
            db.stores.insert(dict(item))
        elif isinstance(item, host2.girlItem):
            db.girl.insert(dict(item))
        return item

    def close_spider(self, spider):
        self.client.close()
        return
